package com.itheima.mapper.impl;

import com.itheima.bean.Student;
import com.itheima.mapper.StudentmyBatis;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentmyBatisimpl implements StudentmyBatis {


    @Override
    public List<Student> selectAll() {
        InputStream resourceAsStream = null;
        List<Student> students =null;
        SqlSession sqlSession =null;
        try {
            resourceAsStream = Resources.getResourceAsStream("StudentConfig.xml");
            SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
            sqlSession = build.openSession(true);

            StudentmyBatis mapper = sqlSession.getMapper(StudentmyBatis.class);
            students = mapper.selectAll();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (resourceAsStream!=null){
                try {
                    resourceAsStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } if (sqlSession!=null){
                try {
                    sqlSession.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


return students;
    }

    @Override
    public Student selectBid(Integer id) {
        return null;
    }

    @Override
    public Integer insert(Student stu) {
        return null;
    }

    @Override
    public Student update(Student stu) {
        return null;
    }

    @Override
    public Student delete(Integer id) {
        return null;
    }
}
